home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / win-os2.swg / 0036_OWL Owner-Drawn List Boxe.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-25  |  8.0 KB  |  262 lines

  1. {
  2. From: mav@dseg.ti.com (Michael Vincze)
  3.  
  4. >I am trying to put together an owner drawn list box that has it's own
  5. >strings. (i.e. Style := Style AND NOT lbs_HasStrings) Windows never calls
  6. >my DrawItem method. I think it may have something to do with the fact
  7. >that I do not know how to tell windows how many items there are in the
  8. >list box. (Maybe it thinks there are none... *shrug*)
  9. >
  10. >If anyone has had this problem, or has knows where I can get source
  11. >examples of owner-drawn list boxes that have their own strings, please
  12. >let me know.
  13.  
  14. Included is an example of an owner drawn list box.  The example is in
  15. two parts:  "ownlist.pas", and "ownlist.res".  The resource file has
  16. been translated with uuencode.
  17.  
  18. { Author:   Michael Vincze  12/27/93                              }
  19. {                                                                 }
  20. { Purpose:  Shows how to create an owner drawn list box.          }
  21. {                                                                 }
  22. { Usage:    Simply run.                                           }
  23.  
  24. program OwnListBox;
  25.  
  26. uses
  27.   WinCrt,
  28.   BWCC,
  29.   Strings,
  30.   WinTypes,
  31.   WinProcs,
  32.   Objects,
  33.   OWindows,
  34.   ODialogs;
  35.  
  36. {$R OwnList}
  37.  
  38. const
  39.   ApplicationName: PChar = 'Owner Draw List Box';
  40.  
  41.   wListBoxId = 200;    { ID of OwnerDrawn ListBox Control }
  42.   wNumItems  = 12;     { Number of items added to ListBox }
  43.  
  44. type
  45.  
  46.   PODListBox = ^TODListBox;
  47.   TODListBox = object (TListBox)
  48.     hIcon1, hIcon2: HICON;
  49.     constructor InitResource  (AParent: PWindowsObject; ResourceID: Integer);
  50.     destructor  Done; virtual;
  51.     procedure   ODADrawEntire (DrawItemStruct: PDrawItemStruct);
  52.     procedure   ODAFocus      (DrawItemStruct: PDrawItemStruct);
  53.     procedure   ODASelect     (DrawItemStruct: PDrawItemStruct);
  54.     procedure   DrawEntry     (DrawItemStruct: PDrawItemStruct);
  55.     procedure   DrawSelf      (DrawItemStruct: PDrawItemStruct);
  56.     end;
  57.  
  58.   TTemplateApplication = object (TApplication)
  59.     procedure InitMainWindow; virtual;
  60.     end;
  61.  
  62.   PTemplateWindow = ^TTemplateWindow;
  63.   TTemplateWindow = object (TDlgWindow)
  64.     AnOwnListBox: PODListBox;
  65.     constructor Init (AParent: PWindowsObject; ATitle: PChar);
  66.     procedure   SetupWindow; virtual;
  67.     function    GetClassName : PChar; virtual;
  68.     destructor  Done; virtual;
  69.     procedure   WMMeasureItem (var Msg: TMessage); virtual wm_First +
  70. wm_MeasureItem;
  71.     procedure   WMDrawItem    (var Msg: TMessage); virtual wm_First +
  72. wm_DrawItem;
  73.     end;
  74.  
  75. constructor TODListBox.InitResource (AParent: PWindowsObject; ResourceID:
  76. Integer);
  77. begin
  78. inherited InitResource (AParent, ResourceId);
  79. hIcon1 := LoadIcon (0, idi_Exclamation);
  80. {
  81. hIcon2 := LoadIcon (0, idi_Question);
  82. }
  83. hIcon2 := LoadIcon (hInstance, 'icon_1')
  84. end;
  85.  
  86. destructor TODListBox.Done;
  87. begin
  88. inherited Done;
  89. DestroyIcon (hIcon2);
  90. end;
  91.  
  92. procedure TODListBox.ODADrawEntire (DrawItemStruct: PDrawItemStruct);
  93. begin
  94. DrawEntry (DrawItemStruct);
  95. if (DrawItemStruct^.itemState and ods_Focus) <> 0 then
  96.   DrawFocusRect (DrawItemStruct^.hDC, DrawItemStruct^.rcItem);
  97. end;
  98.  
  99. procedure TODListBox.ODAFocus (DrawItemStruct: PDrawItemStruct);
  100. begin
  101. DrawFocusRect (DrawItemStruct^.hDC, DrawItemStruct^.rcItem);
  102. end;
  103.  
  104. procedure TODListBox.ODASelect (DrawItemStruct: PDrawItemStruct);
  105. begin
  106. DrawEntry (DrawItemStruct);
  107. if (DrawItemStruct^.itemState and ods_focus) <> 0 then
  108.   DrawFocusRect (DrawItemStruct^.hDC, DrawItemStruct^.rcItem);
  109. end;
  110.  
  111. procedure TODListBox.DrawSelf (DrawItemStruct: PDrawItemStruct);
  112. begin
  113. with DrawItemStruct^ do
  114.   begin
  115.   if (itemAction and oda_DrawEntire) <> 0 then
  116.     ODADrawEntire (DrawItemStruct)
  117.   else if (itemAction and oda_Focus) <> 0 then
  118.     ODAFocus (DrawItemStruct)
  119.   else if (itemAction and oda_Select) <> 0 then
  120.     ODASelect (DrawItemStruct)
  121.   end;
  122. end;
  123.  
  124. procedure TODListBox.DrawEntry (DrawItemStruct: PDrawItemStruct);
  125. var
  126.   dwColor : Word;
  127.   szString: array [0..100] of Char;
  128.   TextRect: TRect;
  129.   bkColor : LongInt;
  130. begin
  131. wvsprintf (szString, 'This is ListBox Entry %d', DrawItemStruct^.itemID );
  132. dwColor := GetTextColor (DrawItemStruct^.hDC);
  133. CopyRect (TextRect, DrawItemStruct^.rcItem);
  134. Inc (TextRect.Left, 50);
  135.  
  136. {
  137. Should create a logbrush that is the background and then fill
  138. if in appropriately.
  139.  
  140. FillRect (DrawItemStruct^.hDC, DrawItemStruct^.rcItem, GetStockObject
  141. (gray_brush));
  142. }
  143. if (DrawItemStruct^.itemState and ODS_SELECTED) <> 0 then
  144.   begin
  145.   SetTextColor (DrawItemStruct^.hDC,  RGB ($ff,0,0));
  146.   if (hIcon1) <> 0 then
  147.     DrawIcon (DrawItemStruct^.hDC,
  148.               DrawItemStruct^.rcItem.left+10,
  149.               DrawItemStruct^.rcItem.top,
  150.               hIcon1);
  151.   end
  152. else
  153.   begin
  154.   if (hIcon2) <> 0 then
  155.     DrawIcon (DrawItemStruct^.hDC,
  156.               DrawItemStruct^.rcItem.left+10,
  157.               DrawItemStruct^.rcItem.top,
  158.               hIcon2);
  159.   end;
  160. DrawText (DrawItemStruct^.hDC,
  161.           szString,
  162.           StrLen (szString),
  163.           TextRect,
  164.           DT_SINGLELINE or DT_VCENTER or DT_LEFT);
  165.  
  166. SetTextColor (DrawItemStruct^.hDC, dwColor);
  167. end;
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. procedure TTemplateApplication.InitMainWindow;
  175. begin
  176. MainWindow := New (PTemplateWindow, Init (nil, 'MainDialog'));
  177. end;
  178.  
  179. constructor TTemplateWindow.Init (AParent: PWindowsObject; ATitle: PChar);
  180. begin
  181. inherited Init (AParent, ATitle);
  182. AnOwnListBox := New (PODListBox, InitResource (@Self, wListBoxId));
  183. end;
  184.  
  185. function TTemplateWindow.GetClassName;
  186. begin GetClassName := 'BorDlg' end;
  187.  
  188. destructor TTemplateWindow.Done;
  189. begin
  190. inherited Done;
  191. end;
  192.  
  193. procedure TTemplateWindow.SetupWindow;
  194. var
  195.   I: Word;
  196. begin
  197. inherited SetupWindow;
  198. for I :=0 to wNumItems - 1 do
  199.   AnOwnListBox^.AddString (MAKEINTRESOURCE( i ));
  200. end;
  201.  
  202. procedure TTemplateWindow.WMMeasureItem (var Msg: TMessage);
  203. var
  204.   lpMeasureItem: PMEASUREITEMSTRUCT;
  205. begin
  206. lpMeasureItem := PMEASUREITEMSTRUCT (Msg.LParam);
  207.  
  208. if (lpMeasureItem^.CtlType = ODT_LISTBOX) and (lpMeasureItem^.CtlID =
  209. wListBoxId) then
  210.   lpMeasureItem^.itemHeight := GetSystemMetrics (SM_CYICON)
  211. else
  212.   DefWndProc (Msg);
  213. end;
  214.  
  215. procedure TTemplateWindow.WMDrawItem (var Msg: TMessage);
  216. begin
  217. if (PDrawItemStruct (Msg.lParam)^.CtlId) = wListBoxId then
  218.   AnOwnListBox^.DrawSelf (PDrawItemStruct (Msg.lParam));
  219. Msg.Result := 1;
  220. end;
  221.  
  222. var
  223.   Application:TTemplateApplication;
  224.  
  225. begin
  226. Application.Init (ApplicationName);
  227. Application.Run;
  228. Application.Done;
  229. end.
  230.  
  231. {---------- snip ---------- snip ---------- snip ---------- snip ----------}
  232.  
  233. begin 644 ownlist.res
  234. M_P, _P$ ,!#H @  *    "    !      0 $      "  @              
  235. M                 (   (    " @ "     @ "  ("   " @(  P,#     
  236. M_P  _P   /__ /\   #_ /\ __\  /___P                          
  237. M    N[N[NP             +N[N[N[N[L          +N[N[N[N[N[NP    
  238. M    N[N[N[N[N[N[NP      "[N[N[N[N[N[N[NP     +N[N[N[N[N[N[N[
  239. MNP    N[N[N[N[N[N[N[N[NP   +N[N[N[N[N[N[N[N[L   N[N[N[N[N[N[
  240. MN[N[N[L  +N[N[N[N[N[N[N[N[N[  "[N[N[N[N[N[N[N[N[NP +   +N[N[
  241. MN[N[N[N[N[NP +N[L+N[N[N[N[N[N[N[L "[N[L+NPNPNPNP"P  L+  N[N[
  242. M"[L NPL+L+L+N["P +N[NP"["P  L+L+  N[  "[N[L "PNPNP +"P"PL  +
  243. M"[N[ + +L+L L N[ +L "P"[NP"[N[N[N[N[N[N[L "[   +N[N[N[N[N[N[
  244. MNP  N[N[N[N[N[N[N[N[N[L  +N[N[N[N[N[N[N[N[N[   +N[N[N[N[N[N[
  245. MN[N[L   "[N[N[N[N[N[N[N[N[    "[N[N[N[N[N[N[N[L     "[N[N[N[
  246. MN[N[N[NP      "[N[N[N[N[N[N[        "[N[N[N[N[N[L          +
  247. MN[N[N[N[L             "[N[N[                             /_P
  248. M#___@ '__@  ?_P  #_X   ?\   #^    ?    #P    X    &    !@
  249. M 0                                          @    8    &    !
  250. MP    \    /@   '\   #_@  !_\   __@  ?_^  ?__\ ___P4 34%)3D1)
  251. M04Q/1P P$*X      ,B !1( $@#: ((  &)O<F1L9P!/=VYE<B!$<F%W;B!,
  252. M:7-T0F]X %T 9@ @ !0  0    -00F]R0G1N $)U='1O;@  $0 0 +@ " #_
  253. M_P   E""3W=N97(@1')A=VX@)DQI<W1B;W@  !( &@"X #8 R "1 *%0@P  
  254. M  !< -H  @!F  (  %!";W)3:&%D90    D "P#( $D 9P !  !00F]R4VAA
  255. M9&4   #_#@!)0T].7S$ ,! 4       !  $ (" 0  0  0#H @   0#_#P#_
  256. M 0 P'#     .  X  8  24-/3E\Q !( !0 !@ !-04E.1$E!3$]'        
  257. +                
  258.  
  259. end
  260.  
  261. {---------- end ---------- end ---------- end ---------- end ----------}
  262.